
package templateswing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//ЛУЧШУЮ РЕАЛИЗАЦИЮ СМ. ТОМ 1 СТР 403-404 !

public class RadioCheck extends JPanel {
    public RadioCheck() {
        String borderTitle="Radio Buttons";
        setBorder(BorderFactory.createTitledBorder(borderTitle));
        final JLabel label;
        add(label=new JLabel("Text"));
        group.add(first);
        group.add(second);
        group.add(third);
        group.add(fourth);
        ActionListener listener=new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                if(first.isSelected())
                    label.setText(first.getText());
                else if(second.isSelected())
                    label.setText(second.getText());
                else if(third.isSelected())
                    label.setText(third.getText());
                else if(fourth.isSelected())
                    label.setText(fourth.getText());
            }
        };
        first.addActionListener(listener);
        second.addActionListener(listener);
        third.addActionListener(listener);
        fourth.addActionListener(listener);
        add(first);
        add(second);
        add(third);
        add(fourth);
    }
    ButtonGroup group=new ButtonGroup();
    JRadioButton first=new JRadioButton("First");
    JRadioButton second=new JRadioButton("Second");
    JRadioButton third=new JRadioButton("Third");
    JRadioButton fourth=new JRadioButton("Fourth");
}
